package com.bing.scanner;

import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;

import java.lang.annotation.Annotation;
import java.util.Set;

public class BingClassPathDefinitionScanner extends ClassPathBeanDefinitionScanner {



    public BingClassPathDefinitionScanner(BeanDefinitionRegistry registry) {
        super(registry);
    }


    @Override
    public Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
        AbstractBeanDefinition definition;
        for (BeanDefinitionHolder holder : beanDefinitions) {
            definition = (AbstractBeanDefinition) holder.getBeanDefinition();
            String beanClassName = definition.getBeanClassName();

            try {
                // definition.getConstructorArgumentValues().addGenericArgumentValue(Class.forName(beanClassName));
                definition.getPropertyValues().add("bingAnnotationClass",Class.forName(beanClassName));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            definition.setBeanClass(BingFactoryBean.class);

        }

        return beanDefinitions;
    }



}
